//
//  LZFTagToolView.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/7/8.
//  Copyright © 2016年 liangzifeng. All rights reserved.
///Users/liangzifeng/GitOschinaDemo/01-LZFBuDeJie(mjfresh完成上拉下拉刷新)/LZFBuDieJie/LZFBuDieJie/Assets.xcassets

#import "LZFTagToolView.h"
#import "LZFAddTagController.h"
#import "LZFNavgationController.h"

@interface LZFTagToolView ()
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIView *bittomView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topViewHeightConstraint;

@property(nonatomic,weak)UIButton *addButton;
@property(nonatomic,strong)NSMutableArray *tagLabels;


@end

@implementation LZFTagToolView

//    [[UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController presentModalViewController:nav animated:YES];

-(NSMutableArray *)tagLabels

{
    if (_tagLabels==nil) {
        _tagLabels=[NSMutableArray new];
    
    }
    return _tagLabels;
}

-(void)awakeFromNib
{
    //创建加号按钮
    UIButton  *addButton=[UIButton buttonWithType:UIButtonTypeCustom];
    [addButton setImage:[UIImage imageNamed:@"tag_add_icon"] forState:UIControlStateNormal];
    [addButton sizeToFit];
    [addButton addTarget:self action:@selector(addClick) forControlEvents:UIControlEventTouchUpInside];
    [self.topView addSubview:addButton];
    self.addButton=addButton;
    
    //添加两个默认的tagLabel
    [self createTagLabels:@[@"哈哈",@"呵呵"]];
    
}
-(void)addClick
{
    WeakSelf;
    
    LZFAddTagController *addTagVC=[[LZFAddTagController alloc]init];
    
    //接受LZFAddTagController传递过来的tags数组数据
    addTagVC.getTagsBlock=^(NSArray *tags){
        [self createTagLabels:tags];
    };
    
    //给LZFAddTagController传递当前LZFTagToolView的tabLabel数据
    addTagVC.tags=[self.tagLabels valueForKeyPath:@"text"];
    
    //弹出addTag控制器
    LZFNavgationController *nav=[[LZFNavgationController alloc]initWithRootViewController:addTagVC];
     [[UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController presentModalViewController:nav animated:YES];
}


/** 创建label */
-(void)createTagLabels:(NSArray *)tags
{
    
    //清空tagLabels数组 cong 从父控件中移除所有的tagLabels
    [self.tagLabels makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tagLabels removeAllObjects];
    
    
    for (int i=0; i<tags.count; i++) {
        UILabel *label=[[UILabel alloc]init];
        label.text=tags[i];
        label.font=[UIFont systemFontOfSize:14];
        label.backgroundColor=[UIColor blueColor];
        label.textAlignment=NSTextAlignmentCenter;
        [self.topView addSubview:label];
        [self.tagLabels addObject:label];
    
        [label sizeToFit];
        
        label.xmg_height=LZFTagButtonHeight;
        label.xmg_width+=2*LZFSmallMargin;
     }
    
    //重新调用layoutSubviews方法
    [self setNeedsLayout];
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //所有的标签 tagLabel
    for (int i=0; i<self.tagLabels.count; i++) {
        UILabel *newLabel=self.tagLabels[i];
        
        if (i==0) {
            newLabel.xmg_x=0;
            newLabel.xmg_y=0;
        }
        else
        {
            UILabel *previousTagLabel=self.tagLabels[i-1];
            CGFloat leftWidth=CGRectGetMaxX(previousTagLabel.frame)+LZFSmallMargin;
            CGFloat rightWidth=self.topView.xmg_width-leftWidth;
            if (rightWidth>newLabel.xmg_width) {
                newLabel.xmg_x=leftWidth;
                newLabel.xmg_y=previousTagLabel.xmg_y;
            }
            else
            {
                newLabel.xmg_x=0;
                newLabel.xmg_y=CGRectGetMaxY(previousTagLabel.frame)+LZFSmallMargin;
            }

        }
        
 
        
    }
    
    //加号按钮
    UILabel *lastLabel=[self.tagLabels lastObject];
    
    if (lastLabel==nil) {
        self.addButton.xmg_x=0;
        self.addButton.xmg_y=0;
    }
    else
    {
        CGFloat leftWidth=CGRectGetMaxX(lastLabel.frame)+LZFSmallMargin;
        CGFloat rightWidth=self.topView.xmg_width-leftWidth;
        if (rightWidth>self.addButton.xmg_width) {
            //不需要换行
            self.addButton.xmg_x=leftWidth;
            self.addButton.xmg_y=lastLabel.xmg_y;
        }
        else
        {  //需要换行
            self.addButton.xmg_x=0;
            self.addButton.xmg_y=CGRectGetMaxY(lastLabel.frame)+LZFSmallMargin;
        }
    }
    
    //计算toolview的高度
    
    self.topViewHeightConstraint.constant=CGRectGetMaxY(self.addButton.frame);
    CGFloat oldHeight = self.xmg_height;
    self.xmg_height=self.topViewHeightConstraint.constant+self.bittomView.xmg_height+LZFSmallMargin;
    self.xmg_y+=oldHeight-self.xmg_height;
    
}



























@end
